#include "LoginLogic.h"

LoginLogic::LoginLogic(void)
{
}

LoginLogic::~LoginLogic(void)
{
}

int LoginLogic::setData(CByteArrays *msg,int socketId)
{
	std::cout<<"\nmsgtype login id:"<<std::endl;
	std::cout<<msg->readInt()<<std::endl;

	char namestr[900] = {0};
	msg->readString(namestr,sizeof( namestr));
	std::cout<<"name:"<<namestr<<std::endl;

	///
	int findName=0;
	char tablename[50]="testuser";
	char sqltmp[400];
	//SELECT * FROM `testuser` WHERE name="google"
	sprintf(sqltmp,"select * from %s",tablename);
	MySqlCenter::GetInstance()->select(sqltmp);
	MYSQL_RES *res = MySqlCenter::GetInstance()->getOperateResult();
	MYSQL_ROW row;
	int t;
	int count = 0;

	
	while(row = mysql_fetch_row(res))
	{
		if(findName==1)
		{
					break;
		}

		for(t=0;t<mysql_num_fields(res);t++)
		{
			char *rowStr=row[t];
			printf("%s  ",rowStr);

			int alen = strlen(rowStr);
			int blen = strlen(namestr);
			if(alen==blen)
			{
				if(strncmp(namestr,rowStr,strlen(rowStr))==0)
				{
					std::cout<<"find login name "<<std::endl;
					std::cout<<rowStr<<std::endl;
					findName=1;
					break;
				}
			}
		}
		//printf(".............\n");
		count ++;
	
	}

	CByteArrays byt;

	//login id
	byt.writeInt(MSGTYPE_LOGIN);
	byt.writeInt(findName);

	MsgHandle::GetInstance()->sendMsg(socketId,byt);

	return 0;
}